#!/bin/bash
user=$(echo $USER)
echo "当前登录用户为${user}"
sudo apt install -y cifs-utils

autoMount(){
    echo "autoMount"
}

mountNetShareDir(){
    /bin/echo -e "输入远程目录目录（例如 //10.0.0.7/Public）："
    read remotePath

    localPath=/media/$(echo ${remotePath}|sed 's/\///g')
    if [ ! -d ${localPath} ]; then
        sudo mkdir -p ${localPath}
    fi
    sudo chown nobody:nogroup ${localPath}
    sudo chmod 777 ${localPath}

    /bin/echo -e "输入用户名："
    read username
    /bin/echo -e "输入密码："
    read password

    /bin/echo -e "是否开机挂载 0:是,1:当次挂载"
    read option
    case ${option} in
    0)
        sudo /bin/echo -e "${remotePath} ${localPath} cifs auto,username=${username},password=${password},rw,file_mode=0777,dir_mode=0777,gid=0,uid=0" >> /etc/fstab
        sudo mount -a
        ;;
    *)
        sudo mount -t cifs ${remotePath} ${localPath} -o username=${username},password=${password},rw,file_mode=0777,dir_mode=0777,gid=0,uid=0
        exit
        ;;
    esac
}

mountNetShareDir


#开机自动挂载
#1.需要新建立一个文件/etc/sambapassword
#username=value
#password=value
#domain=value
#2.在文件/etc/fstab文件中新加一行：
#
#//10.0.0.7/Public /home/binaryhat/Public cifs credentials=/etc/sambapasswd 0 0
#遇到权限问题，可能是你所在的用户组没有相应权限，在credentials=/etc/sambapasswd,gid=998,uid=998 0 0
#sudo mount -a